package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.Res;
import com.example.mybatisplusdemo.model.domain.User;
import com.example.mybatisplusdemo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-17
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );


    @Autowired
    private UserService userService;


    /**
    * 描述：根据Id 查询
    *
    */
//    @Autowired
//    private UserMapper userMapper;
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
//        User user = userService.getById(id);
//        return JsonResponse.success(user);
//    }
    @PostMapping("/add")
    public Res<User> add (@RequestBody User user){
        //判断用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("useraccount",user.getUseraccount());

        User u = userService.getOne(queryWrapper);
        if(u!=null){
            return Res.error("用户已存在");
        }

        //设置添加时间
//        Timestamp timestamp = new Timestamp(new Date().getTime());
//        user.setAdd_time(timestamp);
        //加密密码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        String hash_password =bCryptPasswordEncoder.encode(user.getUserpsd());

        user.setUserpsd(hash_password);

//        int id = (int)userService.count();
//        user.setUserid(id + 1);
        boolean res = userService.save(user);
        if(!res){
            return Res.error("添加失败");
        }
        return Res.success("添加用户成功",user);
    }

    @PostMapping("/edit")
    public Res<User> edit(@RequestBody User user){
        boolean res = userService.updateById(user);
        if(!res){
            return Res.error("编辑用户信息失败");
        }
        return Res.success("编辑用户信息成功",user);
    }

    @DeleteMapping("/delete/{id}")
    public Res<Object> delete(@PathVariable int id){
        boolean res = userService.removeById(id);
        if(!res){
            return Res.error("删除失败");
        }
        return Res.success("删除成功",id);
    }

    @GetMapping("/getid/{id}")
    public Res<User> getId(@PathVariable int id){
        User user = userService.getById(id);
        if(user!=null){
            return Res.success("获取数据成功",user);
        }
        return Res.error("获取数据失败");
    }

    @GetMapping("/page")
    public Res<Object> page(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int pageSize,
                            @RequestParam(defaultValue = "") String username
//                            @RequestParam(defaultValue = "") String useraccount
    ){
        Page<User> userPage = new Page<>(page,pageSize);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
//                .or().like("useraccount",useraccount);

        return Res.success("获取数据成功",userService.getPage(userPage,queryWrapper));
    }

    @PostMapping("/disabled")
    public Res<Object> disabledById(@RequestBody User user){
        boolean res =userService.updateById(user);
        if(res){
            return Res.success("禁用成功",user);
        }
        return Res.error("禁用失败");
    }

    @PostMapping("/enable")
    public Res<Object> enableById(@RequestBody User user){
        boolean res =  userService.updateById(user);
        if(res){
            return Res.success("启用成功",user);
        }
        return Res.error("启用失败");
    }





}

